package cn.tedu.datamanager.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/product")
@Api(tags = "06. 商品图片管理模块")
public class ProductUploadController {
    @PostMapping("/upload")
    @ApiOperation("上传商品图片")
    public String upload(MultipartFile pic) throws IOException {
        String fileName=pic.getOriginalFilename();
        String suffix=fileName.substring(fileName.lastIndexOf("."));
        fileName= UUID.randomUUID() +suffix;

        String dirPath="D:/Tedu/tedu/data-management-system/files";
        File dirFile=new File(dirPath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        String filePath=dirPath+"/"+fileName;
        pic.transferTo(new File(filePath));
        return "/"+fileName;
    }

    @PostMapping("/remove")
    @ApiOperation("移除旧的商品图片")
    public void remove(String url){
        System.out.println("url = " + url);
        new File("D:/Tedu/tedu/data-management-system/files"+url).delete();
    }
}
